/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.maktoub.project.service.impl.provider;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maktoub.apiclientstart.Utils.SignUtils;
import com.maktoub.common.model.baseresponse.CheckOkMes;
import com.maktoub.common.model.entity.User;
import com.maktoub.common.service.RemoteUserService;
import com.maktoub.project.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;

@Service
@DubboService
public class RemoteUserServiceImpl implements RemoteUserService {
    @Autowired
    private UserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public CheckOkMes checkMes(String accessKey, String timestamp,String random, String body,String sign) {

//         判断ak,是否存在于用户表中
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                    .eq(User::getAccessKey, accessKey)
                    ;
            User res = userService.getOne(wrapper);
            if (res == null) {
                return new CheckOkMes(HttpStatus.HTTP_UNAUTHORIZED,"不具备权限！");
            }


//        判断日期
//        String dateStr = "2020-01-23T12:23:56";
            DateTime dt = DateUtil.parse(timestamp);
            LocalDateTime old = LocalDateTimeUtil.of(dt);
            Duration between = LocalDateTimeUtil.between(LocalDateTime.now(), old);
            long minutes = between.toMinutes();
            if (minutes > 5) {//请求超过五分钟
                return new CheckOkMes(HttpStatus.HTTP_UNAUTHORIZED,"请求超时！");
            }


//          判断随机数是否还在redis
            Long expire = redisTemplate.getExpire("random:"+random);
            if (expire<0) {
                return new CheckOkMes(HttpStatus.HTTP_UNAUTHORIZED,"权限过期！");
            }


//          签名
            String secrectKey = res.getSecrectKey();//表中得到
            String signAgain = SignUtils.genSign(body, random,secrectKey);
            if (signAgain==null||!signAgain.equals(sign)) {
                return new CheckOkMes(HttpStatus.HTTP_UNAUTHORIZED,"请求校验未通过！");
            }
            return new CheckOkMes(HttpStatus.HTTP_OK,"ok");
    }
}
